package com.example.myapplication.ui.download

import androidx.room.ColumnInfo
import androidx.room.Entity
import com.example.myapplication.utils.DiffUIDataUtil
import com.example.myapplication.ui.FileBean
import java.util.*
/**
 * 文件下载表
 */
@Entity(tableName = "file_download_table")
class FileDownloadBean : FileBean, DiffUIDataUtil.UiDataDiffer<FileDownloadBean> {

    companion object {
        const val DOWNLOAD_READY: Byte = 3
        const val DOWNLOAD_RUNNING: Byte = 1
        const val DOWNLOAD_ERROR: Byte = 2
        const val DOWNLOAD_FINISH: Byte = 4
    }

    @ColumnInfo(name = "url")
    var url: String? = null

    @ColumnInfo(name = "downloadState")
    var downloadState: Byte = DOWNLOAD_READY

    @ColumnInfo(name = "downloadSize")
    var downloadSize: Long = 0

    @ColumnInfo(name = "downloadSpeed")
    var downloadSpeed: Long = 0

    constructor() : super(UUID.randomUUID().toString())
    constructor(idx: String) : super(idx)
    constructor(bean: FileDownloadBean) : super(bean) {
        this.url = bean.url
        this.downloadState = bean.downloadState
        this.downloadSize = bean.downloadSize
    }

    override fun toString(): String {
        return "(${super.toString()} downloadState=$downloadState, downloadSize=$downloadSize)"
    }

    fun set(bean: FileDownloadBean): FileDownloadBean {
        super.set(bean)
        this.url = bean.url
        this.downloadState = bean.downloadState
        this.downloadSize = bean.downloadSize
        return this
    }

    fun getCopy(bean: FileDownloadBean): FileDownloadBean {
        val copy = FileDownloadBean(this)
        copy.set(bean)
        return copy
    }

    override fun isSame(old: FileDownloadBean): Boolean {
        return this.uuid.equals(old.uuid) && this.id == (this.id)
    }

    override fun isUiContentSame(old: FileDownloadBean): Boolean {
        return (this.deleted == old.deleted &&
                this.enabled == old.enabled &&
                this.downloadSize == old.downloadSize &&
                this.downloadState == old.downloadState &&
                this.state.equals(old.state))

    }
}